Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Add Health Checks #48

Merged
merged 4 commits into from
Nov 28, 2023
Merged

[FEATURE] Add Health Checks #48

merged 4 commits into from
Nov 28, 2023

Conversation

robinmanuelthiel
Copy link
Member

@robinmanuelthiel robinmanuelthiel commented Nov 27, 2023

Health Checks

The library automatically includes a health check endpoint at /healthz, which checks the basic health of the service.

You can add additional health checks to the default setup.

public void ConfigureServices(IServiceCollection services)
{
    services.AddDefaultSetup(_options);

    // Health Checks
    services
      .AddHealthChecks()
      .AddCheck("MyHealthCheck", () => HealthCheckResult.Healthy("Everything is fine.")
      .AddCheck("MyOtherHealthCheck", MyHealthChecker) // Implement IHealthCheck
      .AddSqlServer("<MY_CONNECTION_STRING>")
      .AddDbContextCheck<SampleDbContext>());

    // ...
}

@robinmanuelthiel
Copy link
Member Author

@SebastianKuesters Do you have a better idea, to expose the Health Checks to the StartupOptions object? They need to be registered to the IHealthChecksBuilder, which only gets exposed via services.AddHealthChecks(). As there are many built-in options like health checks for EF Core, I would not like to create a helper method for all of them in StartupOptions. In most cases, this is not even possbile, as StartupOptions can not expose an IHealthChecksBuilder, as it is not generated at this point.

@SebastianKuesters
Copy link
Member

@robinmanuelthiel one suggestion as alternative would be the following which you call in the constructor:

public Startup(IConfiguration configuration)
{
    Configuration = configuration;

    _options = new StartupOptions();

    // Middleware
    _options
      .AddMiddleware<ApiExceptionFilter>();

    // Add Swagger
    _options
        .AddOpenApi("v1")
        .WithApiGroup("public", "Fancy API", "This is my fancy API.")
        .WithSecurityScheme(SecuritySchemeDefaults.JwtBearer);

    // Add Monitoring
    _options
        .AddMonitoring()
        .WithMeter(Observability.Meter.Name)
        .WithApplicationInsights(Configuration["AzureApplicationInsightsConnectionString"])
        .WithPrometheus();
    
    // OPTION A: Add Health Checks
    _options.AddHealthChecks(healthCheckBuilder => {
        healthCheckBuilder
            .AddCheck("MyHealthCheck", () => HealthCheckResult.Healthy("Everything is fine.")
            .AddCheck("MyOtherHealthCheck", MyHealthChecker) // Implement IHealthCheck
            .AddSqlServer("<MY_CONNECTION_STRING>")
            .AddDbContextCheck<SampleDbContext>();
    });

   // OPTION B: Configure Health Checks
    _options.ConfigureHealthChecks(healthCheckBuilder => {
        healthCheckBuilder
            .AddCheck("MyHealthCheck", () => HealthCheckResult.Healthy("Everything is fine.")
            .AddCheck("MyOtherHealthCheck", MyHealthChecker) // Implement IHealthCheck
            .AddSqlServer("<MY_CONNECTION_STRING>")
            .AddDbContextCheck<SampleDbContext>();
    });
}

So, as we are not able to access the IHealthCheckBuilder in the constructor, because we need to access serviceCollection to create it, we just ask for the config in the constructor and we will call this config later from AddDefaultSetup internally.

I vote for Option B, as this is sth. that I know as .NET developer to have this ConfigureXYZ() functions where I pass a config callback in.

@robinmanuelthiel
Copy link
Member Author

I like these Options a lot. B) is also my favorite. Thanks!

@robinmanuelthiel
Copy link
Member Author

@SebastianKuesters can you pls review again?

@robinmanuelthiel
Copy link
Member Author

I also added docs on how to achieve this

image

Copy link

Code Coverage

Package Line Rate Branch Rate Complexity
Wemogy.AspNet 7% 11% 224
Summary 7% (35 / 526) 11% (17 / 152) 224

Copy link
Member

@SebastianKuesters SebastianKuesters left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :)

@robinmanuelthiel robinmanuelthiel merged commit 22e8f1f into main Nov 28, 2023
4 checks passed
@robinmanuelthiel robinmanuelthiel deleted the feature/health-checks branch November 28, 2023 10:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants